﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace QQMusic.Helper
{
    public class VisualHelper
    {
        /// <summary>
        /// 获取父可视对象中第一个指定类型的子可视对象
        /// </summary>
        /// <typeparam name="T">可视对象类型</typeparam>
        /// <param name="parent">父可视对象</param>
        /// <returns>第一个指定类型的子可视对象</returns>
        public static T GetChild<T>(Visual parent) where T : Visual
        {
            T child = default;
            int visuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < visuals; i++)
            {
                Visual visual = VisualTreeHelper.GetChild(parent, i) as Visual;
                child = visual as T;
                if (child == null)
                    child = GetChild<T>(visual);
                else break;
            }
            return child;
        }

        /// <summary>
        /// 指定控件名称获取子控件
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="parent">父对象</param>
        /// <param name="childName">子控件名称</param>
        /// <returns>返回第一个根据名称找到的控件</returns>
        public static T GetChild<T>(DependencyObject parent, string childName) where T : DependencyObject
        {
            if (parent == null) return null;
            T foundChild = null;
            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childrenCount; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                T childType = child as T;
                if (childType == null)
                {
                    foundChild = GetChild<T>(child, childName);
                    if (foundChild != null) break;
                }
                else if (!string.IsNullOrEmpty(childName))
                {
                    var frameworkElement = child as FrameworkElement;
                    if (frameworkElement != null && frameworkElement.Name == childName)
                    {
                        foundChild = (T)child;
                        break;
                    }
                }
                else
                {
                    foundChild = (T)child;
                    break;
                }
            }
            return foundChild;
        }

        /// <summary>
        /// 获取指定对象为T的控件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static List<T> GetChildList<T>(DependencyObject parent) where T : DependencyObject
        {
            if (parent == null) return null;
            List<T> foundChild = new List<T>();
            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childrenCount; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                T childType = child as T;
                if (childType == null)
                {
                    var childList = GetChildList<T>(child);
                    if (childList != null && childList.Count > 0)
                    {
                        foreach (var c in childList)
                        {
                            foundChild.Add(c);
                        }
                    }
                }
                else
                {
                    foundChild.Add((T)child);
                }
            }
            return foundChild;
        }
    }
}
